(function () {

	var WebSocketServer = require('ws').Server;
	var wss = new WebSocketServer({ port: 11451 });
	wss.on('connection', function (ws) {
		ws.on('message', function (message) {
			//	console.log('received: %s', message);
			if (message) {
				if (message == 'xujinyue') {
					console.log('received: %s', message);
				}
				else {
					var data = JSON.parse(message);
					console.log(data);
					if (data.name) {
						//get
						if (data.get == 'name') {
							var fs = require('fs');
							console.log(data);
							fs.exists('./playerdata/' + data.name + '.txt', (exists) => {
								if (exists) ws.send(JSON.stringify({ name: true, asname: true }));
								else ws.send(JSON.stringify({ name: true, asname: false }));
							});

						}
						else if (data.get == 'key') {
							var fs = require('fs');
							fs.readFile('./playerdata/' + data.name + '_key.txt', 'utf-8', function (err, dt) {
								if (err) {
									throw err;
								}
								var dt1=dt;
								fs.readFile('./../Program Files/Apache Software Foundation/Tomcat 10.0/webapps/app/game/version.js', 'utf-8', function (err, dt) {
									if (err) {
										throw err;
									}
									var version1 =dt;
									var yes=false;
									console.log(version1.slice(0,100));
									if(version1.slice(0,100).indexOf(data.version)!=-1) yes=true;
									if (data.key == dt1) ws.send(JSON.stringify({ key: true, askey: true ,asversion:yes}));
									else ws.send(JSON.stringify({ key: true, askey: false ,asversion:yes}));
								});
								
							});
						}
						else if (data.get == 'giftkey') {
							var fs = require('fs');
							fs.exists('./giftkey/' + data.name + '.txt', (exists) => {
								if (exists){
									fs.readFile('./giftkey/' + data.name + '.txt', 'utf-8', function (err, dt) {
										if (err) {
											throw err;
										}
										dt=JSON.parse(dt);
										console.log(dt);
										if(dt[0]>=dt[1]) ws.send(JSON.stringify({ giftkey: true, asgiftkey: false}));
										else{
											dt[0]++;
											ws.send(JSON.stringify({ giftkey: true, asgiftkey: true}));
											fs.writeFile('./giftkey/' + data.name + '.txt', JSON.stringify(dt), function (err) {
												if (err) {
													throw err;
												}
												console.log('giftkey上传完毕.');
											});
										}
									});
								}
								else ws.send(JSON.stringify({ giftkey: true, asgiftkey: false }));
							});
							
						}
						else if (data.get == 'config') {
							var fs = require('fs');
							fs.readFile('./playerdata/' + data.name + '.txt', 'utf-8', function (err, dt) {
								if (err) {
									throw err;
								}
								console.log(dt);
								ws.send(dt);
							});
						}
						else if (data.get == 'existname') {
							var fs = require('fs');
							fs.readFile('./existplayer/playername.txt', 'utf-8', function (err, dt) {
								if (err) {
									throw err;
								}
								var list = dt.split(',');
								var yes = false;
								console.log(list);
								for (var i = 0; i < list.length; i++) {
									if (list[i] === data.name) {
										yes = true;
										break;
									}

								}
								if (!yes) ws.send(JSON.stringify({ existname: true, asname: true }));
								else ws.send(JSON.stringify({ existname: true, asname: false }));
							});
						}
						else if (data.get == 'existCNname') {
							var fs = require('fs');
							fs.readFile('./existplayer/playerCNname.txt', 'utf-8', function (err, dt) {
								if (err) {
									throw err;
								}
								var list = dt.split(',');
								var yes = false;
								console.log(list);
								for (var i = 0; i < list.length; i++) {
									if (list[i] === data.name) {
										yes = true;
										break;
									}

								}
								if (!yes) ws.send(JSON.stringify({ existCNname: true, asCNname: true }));
								else ws.send(JSON.stringify({ existCNname: true, asCNname: false }));
							});
						}
						//put
						else if (data.put == 'existname') {
							var fs = require('fs');
							console.log(data.name);
							fs.writeFile('./existplayer/playername.txt', data.name + ",", { 'flag': 'a' }, function (err) {
								if (err) {
									throw err;
								}
								console.log('existname上传完毕.');
								ws.send('existnameover');
							});
						}
						else if (data.put == 'existCNname') {
							var fs = require('fs');
							console.log(data.name);
							fs.writeFile('./existplayer/playerCNname.txt', data.name + ",", { 'flag': 'a' }, function (err) {
								if (err) {
									throw err;
								}
								console.log('existCNname上传完毕.');
								ws.send('existCNnameover');
							});
						}
						else if (data.put == 'config') {
							var fs = require('fs');
							console.log(typeof data.config)
							if(typeof data.config!='object'||data.config==null) return;
							var str = JSON.stringify({ config: data.config });
							fs.writeFile('./playerdata/' + data.name + '.txt', str, function (err) {
								if (err) {
									throw err;
								}
								console.log('config上传完毕.');
								ws.send('configover');
							});
							if (data.character&&data.result&&data.data) {
								var fs = require('fs');
								fs.exists('./statistics/' + data.character + '.txt', (exists) => {
									if (exists) {
										fs.readFile('./statistics/' + data.character + '.txt', 'utf-8', function (err, dt) {
											var statistics = JSON.parse(dt);
											console.log(statistics);
											if (data.result) statistics[0]++;
											else statistics[1]++;
											statistics[2] += data.data[0];
											statistics[3] += data.data[1];
											statistics[4] += data.data[2];
											statistics[5] += data.data[3];
											statistics[6] += data.data[4];
											console.log(statistics);
											fs.writeFile('./statistics/' + data.character + '.txt', JSON.stringify(statistics), function (err) {
												if (err) {
													throw err;
												}
												console.log('statistics上传完毕.');
											});
											if (err) {
												throw err;
											}
										});
									}
									else {
										var list = [];
										if (data.result) list = [1, 0];
										else list = [0, 1];
										list = list.concat(data.data);
										console.log(list);
										fs.writeFile('./statistics/' + data.character + '.txt', JSON.stringify(list), function (err) {
											if (err) {
												throw err;
											}
											console.log('statistics上传完毕.');
										});
									}
								});
							}
						}
						else if (data.put == 'key') {
							var fs = require('fs');
							fs.writeFile('./playerdata/' + data.name + '_key.txt', data.key, function (err) {
								if (err) {
									throw err;
								}
								console.log('key上传完毕.');
								ws.send('keyover');
							});
						}
					}
					else {
						ws.send('noplayer');
					}
				}

			}
			/*	var fs = require('fs');
				fs.writeFile('./playerdata/try4.txt', message, function (err) {
					if (err) {
						throw err;
					}
					console.log('传输完毕.');
					fs.readFile('./playerdata/try4.txt', 'utf-8', function (err, data) {
						if (err) {
							throw err;
						}
					});
				});*/

		});
		ws.send('xujinyue')
	});
}());
